<template>
  <reportDetail v-if="$route.query.reportId"></reportDetail>
  <div class="app-container" v-else>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      
    >
      <el-form-item :label="$t('report_name')" prop="reportName">
        <el-input
          v-model="queryParams.reportName"
          :placeholder="$t('please_enter', { name: $t('report_name') })"
          :show-word-limit="true"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('calculation_time')" prop="calculationTime">
        <el-date-picker
          clearable
          v-model="queryParams.calculationTime"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('please_select', { name: $t('calculation_time') })"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('payroll_cycle')" prop="salaryCycleName">
        <el-input
          v-model="queryParams.salaryCycleName"
          :placeholder="$t('please_enter', { name: $t('payroll_cycle') })"
          :show-word-limit="true"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('payroll_group')" prop="salaryGroupId">
        <el-select
          v-model="queryParams.salaryGroupId"
          :placeholder="$t('please_select', { name: $t('payroll_group') })"
          clearable
        >
          <el-option
            v-for="dict in salaryGroupOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('operator')" prop="operator">
        <el-input
          v-model="queryParams.operator"
          :placeholder="$t('please_enter', { name: $t('operator') })"
          :show-word-limit="true"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{ $t("search") }}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t("reset")
        }}</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="salaryReportList"
      @selection-change="handleSelectionChange"
      :max-height="tableHeight"
    >
      <el-table-column
        :label="$t('report_name')"
        align="center"
        prop="reportName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('calculation_date')"
        align="center"
        prop="calculationTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.calculationTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('calculation_type')"
        align="center"
        prop="salaryCalculationType"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.salary_calculation_type"
            :value="scope.row.salaryCalculationType"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('payroll_cycle')"
        align="center"
        prop="salaryCycleName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('payroll_group')"
        align="center"
        prop="salaryGroupId"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="salaryGroupOptions"
            :value="scope.row.salaryGroupId"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('heacounts_of_calcualtion')"
        align="center"
        prop="calculationNumberPeople"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('total_net_payment_of_salary')"
        align="center"
        prop="calculationTotalMoney"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('operator')"
        align="center"
        prop="operator"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('download_time')"
        align="center"
        prop="downloadTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.downloadTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('action')"
        align="center"
        class-name="small-padding fixed-width"
        width="160"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['salary:salaryReport:view']"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >{{ $t("view") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['salary:salaryReport:delete']"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >{{ $t("delete") }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-sizes="[10,20,30,50,100, 200]"
    />

    <!-- 添加或修改薪酬报告对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1200px"
      append-to-body
    >
      <el-form size="small" :inline="true" label-width="auto">
        <el-form-item
          :label="item.paramName"
          :prop="item.paramKey"
          v-for="(item, index) in form.salaryReportHeaderList"
          :key="index"
        >
          <el-input
            v-model="item.value"
            :placeholder="`${$t('please_enter',{name:''})}${item.paramName}`"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >{{ $t("search") }}</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
            $t("reset")
          }}</el-button>
        </el-form-item>
      </el-form>

      <el-table
        v-loading="loading"
        :data="salaryReportList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          :label="$t('report_name')"
          align="center"
          prop="reportName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          :label="$t('calculation_date')"
          align="center"
          prop="calculationTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.calculationTime, "{y}-{m}-{d}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('calculation_type')"
          align="center"
          prop="salaryCalculationType"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.salary_calculation_type"
              :value="scope.row.salaryCalculationType"
            />
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('payroll_cycle')"
          align="center"
          prop="salaryCycleName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          :label="$t('payroll_group')"
          align="center"
          prop="salaryGroupId"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="salaryGroupOptions"
              :value="scope.row.salaryGroupId"
            />
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('heacounts_of_calcualtion')"
          align="center"
          prop="calculationNumberPeople"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          :label="$t('total_net_payment_of_salary')"
          align="center"
          prop="calculationTotalMoney"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          :label="$t('operator')"
          align="center"
          prop="operator"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          :label="$t('download_time')"
          align="center"
          prop="downloadTime"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.downloadTime, "{y}-{m}-{d} {h}:{i}:{s}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('action')"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              >{{ $t("view") }}</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              >{{ $t("delete") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        :page-sizes="[10,20,30,50,100, 200]"
      />
    </el-dialog>
  </div>
</template>

<script>
import {
  listSalaryReport,
  getSalaryReport,
  delSalaryReport,
  addSalaryReport,
  updateSalaryReport,
} from "@/api/salary/salaryReport";
import reportDetail from "./detail.vue";
import { listAllSalaryGroup } from "@/api/salary/salaryGroup";
export default {
  name: "SalaryReport",
  dicts: ["salary_calculation_type"],
  components: {
    reportDetail,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 薪酬报告表格数据
      salaryReportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reportName: null,
        calculationTime: null,
        salaryCycleName: null,
        salaryGroupId: null,
        operator: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      salaryGroupOptions: [],

      detailParams: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  created() {
    if (this.$route.query.id) {
      this.queryParams.salaryGroupId = this.$route.query.id;
    }
    this.getList();
    this.listAllSalaryGroup();
  },
  methods: {
    /** 查询薪酬报告列表 */
    getList() {
      this.loading = true;
      listSalaryReport(this.queryParams).then((response) => {
        this.salaryReportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /*查询所有薪酬组*/
    listAllSalaryGroup() {
      listAllSalaryGroup().then((res) => {
        this.salaryGroupOptions = res.data.map((item) => {
          return {
            value: item.id,
            label: item.groupName,
            raw: {
              listClass: "",
            },
          };
        });
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        salaryCalculationId: null,
        reportName: null,
        calculationTime: null,
        salaryCalculationType: null,
        salaryCycleName: null,
        salaryGroupId: null,
        groupName: null,
        calculationNumberPeople: null,
        calculationTotalMoney: null,
        operator: null,
        downloadTime: null,
        isDownload: null,
        confirmedTime: null,
        confirmedBy: null,
        detailJson: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /* 详情操作 */
    handleView(row) {
      this.$router.push(`/salary/salaryReport?reportId=${row.id}`);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateSalaryReport(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("operation_successful"));
              this.open = false;
              this.getList();
            });
          } else {
            addSalaryReport(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("operation_successful"));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(this.$t("delete_data"))
        .then(function () {
          return delSalaryReport(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t("operation_successful"));
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "salary/salaryReport/export",
        {
          ...this.queryParams,
        },
        `salaryReport_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
